import Post
import org.jsecurity.SecurityUtils
class PostService {
    def securityService
    boolean transactional = true

    def getRecentPosts() {
        def user = securityService.getCurrentUser()
        if(user){
            def posts = Post.withCriteria {
                le 'displayDate', new Date()
                eq 'privce',false
                if(!SecurityUtils.subject.hasRole("SUPER_ADMIN")){
                    eq 'school',user.schoolId
                }
                maxResults 10
                order 'displayDate', 'desc'
            }
            return posts
        }else{
            return new ArrayList()
        }

        
    }

    def getDisplayablePosts(max, offset) {
        max = Math.min(max?.toInteger() ?: 10, 100)
        offset = offset?.toInteger() ?: 0


        def posts = Post.withCriteria {
            le 'displayDate', new Date()
            eq 'privce',false
            maxResults max
            firstResult offset
            order 'displayDate', 'desc'
        }

        def postsList = Post.withCriteria {
            le 'displayDate', new Date()
            eq 'privce',false
        }
        [posts: posts, totalPosts: postsList.size()]
    }

    def getPersonDisplayablePosts(user,author,max, offset){
        max = Math.min(max?.toInteger() ?: 10, 100)
        offset = offset?.toInteger() ?: 0
        
        def total
        def posts
        if(user){
            if(SecurityUtils.subject.hasRole("SUPER_ADMIN")){
                total = Post.countBySiteAndPrivce(author.site,false)
                posts = Post.withCriteria {
                    eq 'site',author.site
                    maxResults max
                    firstResult offset
                    order 'displayDate', 'desc'
                }
                return  [posts: posts, totalPosts: total, site:Site.findByAuthor(author)]
            }

            if(user in Parent){
                // 看到的是非私有的其他人的blog，包括自己的也是如此
                if(user.id == author.id || user.student.id == author.id){
                    total = Post.countBySite(user.student.site)
                    posts = Post.withCriteria {
                        eq 'site',user.student.site
                        maxResults max
                        firstResult offset
                        order 'displayDate', 'desc'
                    }
                    return [posts: posts, totalPosts: total, site:Site.findByAuthor(user.student)]
                }
            }
        

            if(author in Classer){
                    total = Post.countByClassesid(author.classesId)
                    posts = Post.withCriteria {
                        eq 'classesid',author.classesId
                        eq 'privce',false
                        maxResults max
                        firstResult offset
                        order 'displayDate', 'desc'
                    }
                    
                    return [posts: posts, totalPosts: total, site:Site.findByAuthor(author),classesStudents:Student.findAllByClassesId(author.hostId)]
            }
        }

        total = Post.countBySiteAndPrivce(author.site,false)
        posts = Post.withCriteria {
            eq 'site',author.site
            eq 'privce',false
            maxResults max
            firstResult offset
            order 'displayDate', 'desc'
        }
        [posts: posts, totalPosts: total, site:Site.findByAuthor(author)]
    }
 

    /**
     * Return all posts for user with pagination support
     * @param user
     * @param params http request parameters passed into controller
     * @return PagedResultList containing all posts for user
     */
    def getUserPosts(user, params) {
        params.max = Math.min(params?.max?.toInteger() ?: 10, 100)
        params.offset = params?.offset?.toInteger() ?: 0
        params.sort = params?.sort ?: "displayDate"
        params.order = params?.order ?: "desc"

        def posts = Post.createCriteria().list(
                max: params.max,
                offset: params.offset,
                sort: params.sort, 
                order: params.order) {
            eq "author", user
        }

        posts
    }

    def getTotalDisplayable() {
        Post.countByDisplayDateLessThanEquals(new Date())
    }

    def getClassSiteByCurrentUserClassId(user) {
        Classer.findAllBySchoolIdAndMaster(user.schoolId,user.hostId)
    }

}
